package org.example.Demo;

public class ShallowCopyExample {
    static class Person implements Cloneable {
        private String name;
        private int age;
        private Address address; // 引用类型

        public Person(String name, int age, Address address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public Address getAddress() {
            return address;
        }

        public void setAddress(Address address) {
            this.address = address;
        }

        @Override
        protected Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    }

    static class Address {
        private String city;

        public Address(String city) {
            this.city = city;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Person original = new Person("Alice", 30, new Address("New York"));
        Person copy = (Person) original.clone();

        // 修改原始对象的引用类型字段
        original.getAddress().setCity("Los Angeles");

        // 打印结果
        System.out.println("Original: " + original.getAddress().getCity()); // Los Angeles
        System.out.println("Copy: " + copy.getAddress().getCity()); // Los Angeles
    }
}